/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/
	
#pragma once

/*-----------------------------------------------------------------------------
	COLOR.H
-----------------------------------------------------------------------------*/

class EColor {
	public:
					EColor			( void );
					EColor			( float rr, float gg, float bb, float aa=1.0f );
					EColor			( const EColor &other );
		explicit	EColor			( const float *floatptr );
		
		float		*Ptr			( void );
		const float	*Ptr			( void ) const;
	
		EColor&		operator =		( const EColor &src );

		EColor		operator -		( void ) const;		
		EColor		operator +		( const EColor &other ) const;
		EColor		operator -		( const EColor &other ) const;
		EColor		operator *		( const float	scale ) const;
		EColor		operator /		( const float	scale ) const;
		bool		operator ==		( const EColor &other ) const;
		bool		operator !=		( const EColor &other ) const;

		bool		IsEqual			( const EColor &other, float eps ) const;		
		EColor		LerpTo			( const EColor &target, float factor ) const;
		
		EColor		Transform		( const EMatrix &xform ) const;
		EColor		Saturate		( uint value ) const;
		float		Luminance		( void ) const;
		
	public:
		static const EColor	kWhite;
		static const EColor	kBlack;
		static const EColor	kRed;
		static const EColor	kGreen;
		static const EColor	kBlue;
		
		static EColor	FromString	( const char *text );
	
	public:
		union {
			float v[4];
			struct { float x, y, z, w; };
			struct { float r, g, b, a; };
		};
	};


/*-----------------------------------------------------------------------------
	COLOR IMPLEMENTATION :
-----------------------------------------------------------------------------*/


inline EColor::EColor( void ) : r(0), g(0), b(0), a(1)
{

}

inline EColor::EColor( float rr, float gg, float bb, float aa/*=1.0f */ ) : r(rr), g(gg), b(bb), a(aa)
{

}

inline EColor::EColor( const EColor &other ) : r(other.r), g(other.g), b(other.b), a(other.a)
{

}

inline EColor::EColor( const float *floatptr ) : r(floatptr[0]), g(floatptr[1]), b(floatptr[2]), a(floatptr[3])
{

}

inline float* EColor::Ptr( void )
{
	return v;
}

inline const float	* EColor::Ptr( void ) const
{
	return v;
}

inline EColor& EColor::operator=( const EColor &src )
{
	r = src.r;
	g = src.g;
	b = src.b;
	a = src.a;
	return *this;
}

inline EColor EColor::operator-( void ) const
{
	return EColor( -r, -g, -b, -a );
}

inline EColor EColor::operator+( const EColor &other ) const
{
	return EColor( r + other.r, g + other.g, b + other.b, a + other.a );
}

inline EColor EColor::operator-( const EColor &other ) const
{
	return EColor( r - other.r, g - other.g, b - other.b, a - other.a );
}

inline EColor EColor::operator*( const float scale ) const
{
	return EColor( r*scale, g*scale, b*scale, a*scale );
}

inline EColor EColor::operator/( const float scale ) const
{
	return EColor( r/scale, g/scale, b/scale, a/scale );
}

inline bool EColor::operator==( const EColor &other ) const
{
	return (r==other.r && g==other.g && b==other.b && a==other.a );
}

inline bool EColor::operator!=( const EColor &other ) const
{
	return !(*this==other);
}